import java.util.concurrent.Semaphore;

/**
 * @author keboom
 * @date 2021/4/29 10:56
 */
public class TwoThreadPrint {
    private int count = 0;
//    private final Object lock = new Object();
//
//    public void turning() throws InterruptedException {
//        Thread even = new Thread(() -> {
//            while (count <= 100) {
//                synchronized (lock) {
//                    System.out.println("偶数: " + count++);
//                    lock.notifyAll();
//                    try {
//                        // 如果还没有结束，则让出当前的锁并休眠
//                        if (count <= 100) {
//                            lock.wait();
//                        }
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//        Thread odd = new Thread(() -> {
//            while (count <= 100) {
//                synchronized (lock) {
//                    System.out.println("奇数: " + count++);
//                    lock.notifyAll();
//                    try {
//                        // 如果还没有结束，则让出当前的锁并休眠
//                        if (count <= 100) {
//                            lock.wait();
//                        }
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//        even.start();
//        // 确保偶数线程线先获取到锁
//        Thread.sleep(1);
//        odd.start();
//    }

    private static Semaphore A = new Semaphore(1);
    // B、C信号量,A完成后开始,初始信号数量为0
    private static Semaphore B = new Semaphore(0);
    private static Semaphore C = new Semaphore(0);

    static class ThreadA extends Thread {
        @Override
        public void run() {
            try {
                for (int i = 0; i < 10; i++) {
                    A.acquire();// A获取信号执行,A信号量减1,当A为0时将无法继续获得该信号量
                    System.out.print("A");
                    B.release();// B释放信号，B信号量加1（初始为0），此时可以获取B信号量
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    static class ThreadB extends Thread {
        @Override
        public void run() {
            try {
                for (int i = 0; i < 10; i++) {
                    B.acquire();
                    System.out.print("B");
                    C.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    static class ThreadC extends Thread {
        @Override
        public void run() {
            try {
                for (int i = 0; i < 10; i++) {
                    C.acquire();
                    System.out.println("C");
                    A.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        new ThreadA().start();
        new ThreadB().start();
        new ThreadC().start();

    }
}
